﻿using IOA.MES.Business;
using IOA.MES.DataAccess.VModel;
using NLog;
using System.Collections.Generic;
using System.Web.Mvc;

namespace IOA.MES.Web.Controllers
{
    public class AllocateOrderController : Controller
    {
        private Logger logger = LogManager.GetCurrentClassLogger();

        #region 调拨管理
        /// <summary>
        /// 调拨管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AllocateOrderMgr()
        {
            return View();
        }

        /// <summary>
        /// 查询订单列表
        /// </summary>
        [HttpPost]
        public JsonResult QueryAllocateOrders(int page, int limit, string toWarehouseCode, string workOrderNo, string allocateNo)
        {
            var total = 0;
            var orders = new AllocateOrderMgr().QueryAllocateOrders(page, limit, out total, toWarehouseCode, workOrderNo, allocateNo, true);
            return Json(new ResultJson<object>(orders, total));
        }

        /// <summary>
        /// 调拨单详情
        /// </summary>
        public ActionResult AllocateOrderDetail(string allocateNo)
        {
            var entity = new AllocateOrderMgr().GetAllocateOrder(allocateNo);
            return View(entity);
        }

        /// <summary>
        /// 获取所有调拨物料
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="limit">条数</param>
        /// <param name="allcateOrderId">调拨单Id</param>
        /// <returns></returns>
        public JsonResult QueryAllocateProducts(int page, int limit, string allcateOrderNo = "")
        {
            if (!string.IsNullOrWhiteSpace(allcateOrderNo))
            {
                var total = 0;
                var products = new AllocateOrderMgr().QueryAllocateProducts(page, limit, out total, allcateOrderNo);
                return Json(new ResultJson<object>(products, total));
            }
            else
            {
                return Json(new ResultJson<object>());
            }
        }
        #endregion

        #region 调拨收货
        public ActionResult AllocateInbound(string allocateNo = "")
        {
            ViewBag.AllocateNo = allocateNo;
            return View();
        }

        /// <summary>
        /// 获取调拨收货物料信息
        /// </summary>
        /// <param name="allcateOrderId">调拨单ID</param>
        /// <param name="batchNo">调拨单号</param>
        [HttpPost]
        public JsonResult GetAllocateProduct(string allcateNo, string batchNo)
        {
            var allocateProduct = new AllocateOrderMgr().GetAllocateProduct(allcateNo, batchNo);
            return Json(new { ErrCode = 200, Data = allocateProduct });
        }

        /// <summary>
        /// 调拨收货入库
        /// </summary>
        [HttpPost]
        public JsonResult AllocateInbound(string allocateNo, List<VInboundProduct> products)
        {
            var allocate = new AllocateOrderMgr().GetAllocateOrder(allocateNo);
            var model = new VInbound
            {
                CreateBy = BaseEnv.UserId,
                InboundType = DataAccess.Enum.InboundType.AllocateInbound,
                InboundTypeNo = allocate.AllocateNo,
                WarehouseCode = allocate.ToWarehouseCode,
                WarehouseName = allocate.ToWarehouse
            };
            products.ForEach(product =>
            {
                product.CreateBy = BaseEnv.UserId;
                product.InventoryType = DataAccess.Enum.InventoryType.Mterial;
            });

            new InboundMgr().Inbound(model, products);

            return Json(new { ErrCode = 200 });
        }
        #endregion

        #region 调拨单批次号同步
        [HttpPost]
        public JsonResult AsyncEsbAllocateOrders(string allocateNo)
        {
            return Json(new { ErrCode = 200 });
        }
        #endregion
    }
}
